<template>
  <div class="content-list">
    <div class="page-header">
      <h2>内容管理</h2>
      <el-button type="primary" @click="handleAddContent">添加内容</el-button>
    </div>
    
    <el-card class="box-card">
      <div class="search-bar">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="标题">
            <el-input placeholder="请输入标题" clearable></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select placeholder="请选择" clearable>
              <el-option label="文章" value="article"></el-option>
              <el-option label="视频" value="video"></el-option>
              <el-option label="课程" value="course"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select placeholder="请选择" clearable>
              <el-option label="已发布" value="published"></el-option>
              <el-option label="草稿" value="draft"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary">查询</el-button>
            <el-button>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          width="200">
        </el-table-column>
        <el-table-column
          prop="cover"
          label="封面"
          width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="50" :src="scope.row.cover || 'https://placeholder.pics/svg/50'">
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column
          prop="type"
          label="类型"
          width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.type === 'article'">文章</el-tag>
            <el-tag type="success" v-else-if="scope.row.type === 'video'">视频</el-tag>
            <el-tag type="warning" v-else-if="scope.row.type === 'course'">课程</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="author"
          label="作者"
          width="120">
        </el-table-column>
        <el-table-column
          prop="publishTime"
          label="发布时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="views"
          label="浏览量"
          width="100">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          width="100">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status === 'published'">已发布</el-tag>
            <el-tag type="info" v-else>草稿</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180">
          <template #default="scope">
            <el-button type="text" size="small" @click="handleView(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'

export default {
  setup() {
    const router = useRouter()
    
    return {
      router
    }
  },
  data() {
    return {
      tableData: [
        {
          id: 1,
          title: '每天30分钟运动的好处',
          cover: 'https://placeholder.pics/svg/50/E74C3C/FFFFFF/文章',
          type: 'article',
          author: '健康专家',
          publishTime: '2023-11-01 10:30:00',
          views: 1250,
          status: 'published'
        },
        {
          id: 2,
          title: '健康饮食指南',
          cover: 'https://placeholder.pics/svg/50/F39C12/FFFFFF/视频',
          type: 'video',
          author: '营养师李明',
          publishTime: '2023-11-05 14:45:00',
          views: 980,
          status: 'published'
        },
        {
          id: 3,
          title: '科学健身课程',
          cover: 'https://placeholder.pics/svg/50/8E44AD/FFFFFF/课程',
          type: 'course',
          author: '教练王强',
          publishTime: '',
          views: 0,
          status: 'draft'
        }
      ],
      total: 3
    }
  },
  methods: {
    handleAddContent() {
      // 跳转到添加内容页面
      this.router.push('/admin/content/add');
    },
    handleView(row) {
      // 跳转到内容详情页面
      this.router.push(`/admin/content/detail/${row.id}`);
    },
    handleEdit(row) {
      // 跳转到内容编辑页面
      this.router.push(`/admin/content/edit/${row.id}`);
    },
    handleDelete(row) {
      // 弹出确认删除对话框
      this.$confirm('确认删除该内容吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里添加删除内容的逻辑
        this.tableData = this.tableData.filter(item => item.id !== row.id);
        this.total -= 1;
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>
.content-list {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-bar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 